﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
using System.IO;
using System.Reflection;

namespace Batman.DLL.UserModule
{
    public class ApplicationDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
    {
        public ApplicationDbContext CreateDbContext(string[] args)
        {

            //var env = "Development";
            var env = "Production";
            IConfigurationRoot configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile($"appsettings.{env}.json")
                .Build();

            var builder = new DbContextOptionsBuilder<ApplicationDbContext>();
            var connStr = configuration.GetConnectionString("Default");
            builder.UseMySql(connStr, sql =>
            {
                sql.EnableRetryOnFailure();
                sql.MigrationsAssembly("Batman.DLL.UserModule");
            });

     
            var production = new ApplicationDbContext(builder.Options);

            return production;
        }
    }
}
